import { ref, reactive } from "vue";
import { deleteNotice, getNoticeList } from "@/api/notice.ts";
import type { NoticeItem } from "@/view/Notice/utils/type.ts";
import { ElMessage } from "element-plus";

export default function useNotice() {

  const dialog = ref()
  const noticeList = ref<NoticeItem[]>([])
  const params = reactive({
    page: 1,
    limit: 10,
    time: ''
  })

  // 获取公告列表
  const handleGetList = async () => {
    const res = await getNoticeList(params)
    noticeList.value = res.data
  }

  // 添加公告
  const handleAdd = () => {
    dialog.value.init(0, {})
  }

  // 编辑公告
  const handleEdit = (row: any) => {
    dialog.value.init(1, row)
  }

  // 删除公告
  const handleDelete = async (id: string) => {
    const res: any = await deleteNotice(id)
    ElMessage({
      type: res.code === 0 ? 'success' : 'error',
      message: res.message
    })
    await handleGetList()
  }

  return {
    params,
    noticeList,
    dialog,
    handleAdd,
    handleGetList,
    handleEdit,
    handleDelete
  }
}
